package com.yzqp.generator.util.generator.java;

import com.yzqp.generator.bean.TableColumnInfo;
import com.yzqp.generator.exception.GenerateException;
import com.yzqp.generator.prop.java.GeneratorBaseProp;
import com.yzqp.generator.service.TableColumnInfoService;
import com.yzqp.generator.util.FileUtil;
import com.yzqp.generator.util.generator.filter.column.TableColumnFilter;
import com.yzqp.generator.util.generator.filter.map.GeneratorMapFilter;
import com.yzqp.generator.util.generator.filter.templedeal.TempleStringDealFilter;
import com.yzqp.generator.util.generator.filter.templedeal.pre.TowIfFunctionNoLine;
import com.yzqp.generator.util.resolve.ResolveContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成文件的基础 工具类
 *
 * @author hejiacheng
 * @date 2022/3/7
 */
public abstract class BaseGenerator {


    @Autowired
    protected GeneratorBaseProp generatorBaseProp;

    @Autowired
    private TableColumnInfoService tableColumnInfoService;

    // 生成的参数
    protected Map<String, Object> map;

    // 过滤列的过滤器 集合
    protected List<TableColumnFilter> tableColumnFilters;

    // 过滤map的过滤器 集合
    protected List<GeneratorMapFilter> generatorMapFilters;

    public void setTableColumnFilters(List<TableColumnFilter> tableColumnFilters) {
        this.tableColumnFilters = tableColumnFilters;
    }

    public void setGeneratorMapFilters(List<GeneratorMapFilter> generatorMapFilters) {
        this.generatorMapFilters = generatorMapFilters;
    }

    /**
     * 生成文件
     */
    public void generatorFile() {
        // 查找列
        List<TableColumnInfo> tableColumnInfoList = this.findTableColumnInfo();
        // 过滤 设置 处理 列集合数据
        tableColumnInfoList = this.filterColumn(tableColumnInfoList);

        // 设置参数
        Map<String, Object> map = new HashMap<>();

        // 过滤 设置 处理 map参数
        map = this.mapFilter(map, tableColumnInfoList);

        // 设置为成员变量
        this.map = map;
        // 读取模板
        String templeString = FileUtil.readTemple(this.getTemplePath());
        // 过滤 设置 处理 temple字符创
        templeString = this.templeStringFilter(templeString);
        System.out.println("模板 : " + templeString);
        // 解析模板
        //String res = ResolveTempletUtil.deal(templeString, map);
        ResolveContext resolveContext = new ResolveContext(templeString, map);
        String res = resolveContext.resolve();
        System.out.println("解析后的文本 : " + res);
        String saveFilePath = this.getGeneratorFilePath();
        // 需要的生成的文件，已经生成过了
        if (FileUtil.exist(saveFilePath)) {
            if (!generatorBaseProp.getCoverFlag()) {
                throw new GenerateException("目标文件已经生成过了！ path : " + saveFilePath);
            }
        }
        FileUtil.writeFile(saveFilePath, res);
    }

    /**
     * 查询表的列
     *
     * @return
     */
    public List<TableColumnInfo> findTableColumnInfo() {
        // 找到表
        List<TableColumnInfo> tableColumnInfoList = tableColumnInfoService.findTableColumnInfo(generatorBaseProp.getDatabaseName(), generatorBaseProp.getTableName());
        if (tableColumnInfoList.size() <= 0) {
            throw new GenerateException("找不到目标表的列");
        }
        return tableColumnInfoList;
    }

    /**
     * 过滤处理 列对象 集合
     */
    public List<TableColumnInfo> filterColumn(List<TableColumnInfo> tableColumnInfoList) {
        for (TableColumnFilter tableColumnFilter : tableColumnFilters) {
            tableColumnInfoList = tableColumnFilter.filter(tableColumnInfoList);
        }
        return tableColumnInfoList;
    }

    /**
     * 过滤处理 map 对象
     */
    private Map<String, Object> mapFilter(Map<String, Object> map, List<TableColumnInfo> tableColumnInfoList) {
        for (GeneratorMapFilter filterItem : generatorMapFilters) {
            map = filterItem.filter(map, tableColumnInfoList);
        }
        return map;
    }

    protected String templeStringFilter(String templeString) {
        List<TempleStringDealFilter> list = new ArrayList<>();

        // 过滤 删除两个if函数中的 换行 空格 符号
        list.add(new TowIfFunctionNoLine());

        // 循环过滤
        for (TempleStringDealFilter templeStringDealFilter : list) {
            templeString = templeStringDealFilter.filter(templeString);
        }
        return templeString;
    }

    /**
     * 获得模板路径
     *
     * @return
     */
    public abstract String getTemplePath();

    /**
     * 获得生成文件路径
     *
     * @return
     */
    protected abstract String getGeneratorFilePath();
}
